//
//  DataBaseCollect.m
//  UI_豆瓣扩展
//
//  Created by apple on 15/11/9.
//  Copyright © 2015年 张磊. All rights reserved.
//

#import "DataBaseCollect.h"
#import "FMDatabase.h"
#import "Movie.h"
@interface DataBaseCollect()

@property (nonatomic, retain)FMDatabase *db;
@end

@implementation DataBaseCollect

+ (instancetype)shareDataBase
{
    static DataBaseCollect *dbCollect = nil;

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        dbCollect = [[DataBaseCollect alloc] init];
    });
    return dbCollect;
}

- (void)openDB
{
    if (self.db != nil ) {
        NSLog(@"数据库已经打开");
        return;
    }
    
    NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSString *fileName = [doc stringByAppendingPathComponent:@"collect.sqlite"];
    NSLog(@"fileName = %@", fileName);
    
    self.db = [FMDatabase databaseWithPath:fileName];
    
    if ([self.db open]) {
        NSLog(@"数据库打开成功");
    }else {
        NSLog(@"数据库打开失败");
    }

}

- (void)createDB
{
    NSString *sql = @"CREATE TABLE IF NOT EXISTS collect(id INTEGER PRIMARY KEY AUTOINCREMENT,movieId TEXT, movieName TEXT, userName TEXT)";
    BOOL result = [self.db executeUpdate:sql];
    if (result) {
        NSLog(@"创建表成功");
    }else{
        NSLog(@"创建用户表失败");
    }

}

- (void)insertMovie:(NSString *)movieId name:(NSString *)movieName userName:(NSString *)userName
{
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO collect(movieId, movieName, userName) VALUES('%@', '%@', '%@')",movieId ,movieName,  userName];
    BOOL result = [self.db executeUpdate:sql];
    if (result) {
        NSLog(@"插入成功");
    }else {
        NSLog(@"插入失败");
    }
}

- (NSMutableArray *)selectMovieCollect:(NSString *)userName MovieName:(NSString *)movieName
{
    NSMutableArray *array = [NSMutableArray array];
    
    NSString *sql = [NSString stringWithFormat:@"SELECT * FROM collect WHERE movieName = '%@' AND userName = '%@'",movieName, userName];
    FMResultSet *resultSet = [self.db executeQuery:sql];
    while ([resultSet next]) {
        NSString *num = [resultSet objectForColumnName:@"movieId"];
        NSString *movieNameStr = [resultSet objectForColumnName:@"movieName"];

        Movie *movie = [[Movie alloc]init];
        movie.movieId = num;
        movie.movieName = movieNameStr;
        
        
        [array addObject:movie];
    }
    
    return array;
}


- (NSMutableArray *)selectMovieArray:(NSString *)userName
{
    NSMutableArray *array = [NSMutableArray array];
    
    NSString *sql = [NSString stringWithFormat:@"SELECT * FROM collect WHERE userName = '%@'", userName];
    FMResultSet *resultSet = [self.db executeQuery:sql];
    while ([resultSet next]) {
        NSString *num = [resultSet objectForColumnName:@"movieId"];
        NSString *nameStr = [resultSet objectForColumnName:@"movieName"];
        
        Movie *movie = [[Movie alloc] init];
        movie.movieId = num;
        movie.movieName = nameStr;
        
        [array addObject:movie];
    }
    
    return array;
}



- (void)deleteMovie:(NSString *)movieName user:(NSString *)userName{
    NSString *sql = [NSString stringWithFormat:@"DELETE FROM collect WHERE movieName = '%@' AND userName = '%@'", movieName,userName];
    BOOL result = [self.db executeUpdate:sql];
    if (result) {
        NSLog(@"删除成功");
    } else {
        NSLog(@"删除失败");
    }
    
}




























@end
